package com.sws.web.controller.student_attendance.capture;

import com.sws.attendance.domain.StuAttendance;
import com.sws.attendance.service.IStuAttendanceService;
import com.sws.capture.domain.CaptureDto;
import com.sws.capture.service.CapRecordService;
import com.sws.capture.util.TimestampConverterUtil;
import com.sws.common.annotation.Anonymous;
import com.sws.common.core.controller.BaseController;
import com.sws.common.core.domain.AjaxResult;
import com.sws.device.domain.DevDevice;
import com.sws.device.mapper.DevDeviceMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@Api(tags = {"设备人脸抓拍端口"})
@RestController
@Slf4j
public class FaceCaptureController extends BaseController {
    @Autowired
    private CapRecordService capRecordService;

    @Autowired
    private IStuAttendanceService stuAttendanceService;
    
    @Autowired
    private DevDeviceMapper devDeviceMapper;

    /**
     * 人脸识别接口，用于处理人脸识别信息并更新或插入考勤记录
     * @param captureDto 包含人脸识别信息的DTO对象
     * @return 返回操作结果，成功或错误
     */
    @ApiOperation("人脸识别接口，用于处理人脸识别信息并更新或插入考勤记录")
    @PostMapping("/api/v1/adapter/lenfocus/face/capture")
    @Anonymous
    public AjaxResult faceCapture(@RequestBody CaptureDto captureDto) {
        log.debug("收到人脸抓拍数据: {}", captureDto);
        
        // 预处理图片URL，替换/progs/html为设备IP
        preprocessImageUrls(captureDto);
        
        boolean b = capRecordService.faceCapture(captureDto);

   /*     //先查询是否已经存在相应的考勤信息记录，如果有，则调用插入逻辑，如果无，则调用更新逻辑{}
        int i = 0 ;
        StuAttendance sa = new StuAttendance();
        sa.setPid(captureDto.getPid());
        sa.setSn(captureDto.getSn());
        // 将时间戳转换为对应的北京时间
        sa.setRecordDate(TimestampConverterUtil.toBeijingDate(captureDto.getTime()));


        //新增考勤信息展示
        if(!capRecordService.ifSaExist(sa)) {
            // 如果信息已存在，则进行初次插入
            // 联查插入：设备表
            sa = capRecordService.faceCaptureDevice(sa);
            //考勤记录表数据处理
            StuAttendance stuAttendance =  capRecordService.stuAttendanceMsgprocess(captureDto,sa);

             i = stuAttendanceService.insertStuAttendance(stuAttendance);
        }
        else {
            //考勤记录表数据处理
            StuAttendance stuAttendance = capRecordService.stuAttendanceMsgprocess(captureDto,sa);
            i = stuAttendanceService.updateStuAttendance(stuAttendance);
        }

        if( i != 0 && b){
            return  AjaxResult.success();
        }else {
            return  AjaxResult.error();
        }

*/          return b  ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 预处理图片URL，将/progs/html替换为设备IP
     * @param captureDto 抓拍数据DTO
     */
    private void preprocessImageUrls(CaptureDto captureDto) {
        if (captureDto == null || captureDto.getSn() == null) {
            return;
        }
        
        // 获取设备信息
        DevDevice device = devDeviceMapper.selectDevDeviceBySn(captureDto.getSn());
        if (device == null || device.getIp() == null || device.getIp().isEmpty()) {
            return;
        }
        
        String ipPrefix = "http://" + device.getIp();
        Map<String, String> data = captureDto.getData();
        
        if (data != null) {
            // 处理各种图片URL
            if (data.containsKey("jpeg_url_rcg")) {
                String url = data.get("jpeg_url_rcg");
                if (url != null && url.contains("/progs/html")) {
                    data.put("jpeg_url_rcg", url.replace("/progs/html", ipPrefix));
                }
            }
            
            if (data.containsKey("jpeg_url_face")) {
                String url = data.get("jpeg_url_face");
                if (url != null && url.contains("/progs/html")) {
                    data.put("jpeg_url_face", url.replace("/progs/html", ipPrefix));
                }
            }
            
            if (data.containsKey("jpeg_url_body")) {
                String url = data.get("jpeg_url_body");
                if (url != null && url.contains("/progs/html")) {
                    data.put("jpeg_url_body", url.replace("/progs/html", ipPrefix));
                }
            }
            
            if (data.containsKey("jpeg_url_frame")) {
                String url = data.get("jpeg_url_frame");
                if (url != null && url.contains("/progs/html")) {
                    data.put("jpeg_url_frame", url.replace("/progs/html", ipPrefix));
                }
            }
        }
    }
}

/**
 {
 "type":11,
 "pid":"040221",
 "time":1750033803,
 "sn":"PFM1254O1009701",
 "name":"邓浩威",
 "age":20,
 "gender":1,
 "work_id":"1001",
 "phone":"13642728885",
 "department":"部门名称",
 "otherInfo":"(null)",
 "category":1,
 "score":90,
 "helmet":0,
 "smoke":0,
 "handed":0,
 "feature":[]
 }
 */